package com.distributed.common.message;

import com.distributed.common.Constant;

import net.sf.json.JSONObject;

public class EnterRoomMessage extends BaseMessage {
	
	public EnterRoomMessage() {
		super();
		setId(Constant.ENTER_ROOM_MESSAGE);
	}

	public EnterRoomMessage(int rid) {
		super();
		setId(Constant.ENTER_ROOM_MESSAGE);
		setRoomId(rid);
	}

	public String getRoomId() {
		return argv.get(Constant.ROOM_ID);
	}

	public void setRoomId(int rid) {
		argv.put(Constant.ROOM_ID, String.valueOf(rid));
	}

	public static String getName() {
		return Constant.ENTER_ROOM_MESSAGE_NAME;
	}

	public static EnterRoomMessage getFromString(String message) {
		JSONObject jsonobject = JSONObject.fromObject(message);
		if (jsonobject == null) {
			return null;
		}
		return getFromJSON(jsonobject);
	}

	public static EnterRoomMessage getFromJSON(JSONObject jsonobj) {
		BaseMessage baseMessage = new EnterRoomMessage();
		BaseMessage baseMsg = BaseMessage.getFromJSON(jsonobj, baseMessage);
		
		if (baseMsg == null || baseMsg.getId() != Constant.ENTER_ROOM_MESSAGE) {
			return null;
		}
		String roomId = baseMsg.getArgvValue(Constant.ROOM_ID);
		if (roomId == null || "".equals(roomId)) {
			return null;
		}
		
		return (EnterRoomMessage) baseMsg;
	}

	public JSONObject toJSON() {
		return super.toJSON();
	}
	
	public String toString() {
		return toJSON().toString();
	}
}
